<template>
  <v-data-table :headers="headers" :items="desserts" sort-by="calories" class="elevation-1">
    <template v-slot:top>
      <v-toolbar flat>
        <v-toolbar-title>核算产品与业务代号映射</v-toolbar-title>
        <v-divider class="mx-4" inset vertical></v-divider>
        <v-spacer></v-spacer>
        <v-dialog v-model="dialog" max-width="800px">
          <template v-slot:activator="{ on }">
            <v-btn color="primary" dark class="mb-2" v-on="on">新增</v-btn>
          </template>
          <v-card>
            <v-card-title>
              <span class="headline">{{ formTitle }}</span>
            </v-card-title>

            <v-card-text>
              <v-container>
                <v-row>
                  <!-- <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.accprdno" label="核算产品号"></v-text-field>
                  </v-col> -->
                  <v-col cols="12" sm="6" md="4">
                  <v-select :items="accprdnoItems" 
                  label="核算产品" 
                  v-model="editedItem.accprdno"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>
                  <!-- <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.accprdpno" label="核算产品属性编号"></v-text-field>
                  </v-col> -->
                  <v-col cols="12" sm="12" md="12">
                    <v-select :items="accprdpnoItems" 
                    label="核算产品属性编号" 
                    v-model="editedItem.accprdpno"
                    item-text="name"
                    item-value="attributes"
                    multiple
                        persistent-hint
                        chips
                    ></v-select>
                    </v-col>

                  
                  <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.seqno" label="序号"></v-text-field>
                  </v-col>

                  
                  <v-col cols="12" sm="6" md="4">
                  <v-select :items="accfaccodeItems" 
                  label="记账要素" 
                  v-model="editedItem.accfaccode"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>

                  <!-- <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.accfaccode" label="记账要素英文名称"></v-text-field>
                  </v-col>
                   <v-col cols="12" sm="6" md="4">
                    <v-text-field v-model="editedItem.accfacchnm" label="记账要素中文名称"></v-text-field>
                  </v-col> -->
                    <v-col cols="12" sm="12" md="12">
                    <v-autocomplete
                      v-model="editedItem.busnoa"
                      :items="busnoaItems"
                      chips
                      clearable
                      hide-details
                      hide-selected
                      item-text="glSubjectInfo"
                      item-value="businessCode"
                      label="请选择业务代号"
                      solo
                    ></v-autocomplete>
                   
                  </v-col>

                   <v-col cols="12" sm="6" md="4">
                  <v-select :items="prinsubflgItems" 
                  label="是否本金科目" 
                  v-model="editedItem.prinsubflg"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>
                   
                  <v-col cols="12" sm="6" md="4">
                  <v-select :items="rcdstsItems" 
                  label="记录状态" 
                  v-model="editedItem.rcdsts"
                  item-text="name"
                  item-value="attributes"
                  ></v-select>
                  </v-col>
                </v-row>
              </v-container>
            </v-card-text>

            <v-card-actions>
              <v-spacer></v-spacer>
              <v-btn color="blue darken-1" text @click="close">取消</v-btn>
              <v-btn color="blue darken-1" text @click="save">保存</v-btn>
            </v-card-actions>
          </v-card>
        </v-dialog>
      </v-toolbar>
    </template>
    <template v-slot:item.actions="{ item }">
      <v-icon small class="mr-2" @click="editItem(item)">mdi-pencil</v-icon>
      <v-icon small @click="deleteItem(item)">mdi-delete</v-icon>
    </template>

      <template v-slot:item.accfacchnm="{ item }">
    
      <span v-for="s in accfaccodeItems" key="s.id" v-if="s.attributes==item.accfaccode">{{s.name}}</span>
    </template>

    <template v-slot:item.rcdsts="{ item }">
    
      <span v-for="s in rcdstsItems" key="s.id" v-if="s.attributes==item.rcdsts">{{s.name}}</span>
    </template>

     <template v-slot:item.prinsubflg="{ item }">
    
      <span v-for="s in prinsubflgItems" key="s.id" v-if="s.attributes==item.prinsubflg">{{s.name}}</span>
    </template>

    <template v-slot:no-data>
      <v-btn color="primary" @click="initialize">Reset</v-btn>
    </template>
  </v-data-table>
</template>

<script>
export default {
  data: () => ({
    rcdstsItems: [],
    prinsubflgItems:[],
    accprdpnoItems:[],
    accprdnoItems:[],
    accfaccodeItems:[],
    busnoaItems:[],
    dialog: false,
    headers: [
      {
        text: "核算产品号",
        align: "start",
        value: "accprdno"
      },
      { text: "序号", value: "seqno" },
      { text: "核算产品属性编号", value: "accprdpno" },
      { text: "记账要素英文名称", value: "accfaccode" },
       { text: "记账要素中文名称", value: "accfacchnm" },
        { text: "业务代号", value: "busnoa" },
           { text: "是否本金科目", value: "prinsubflg" },
      { text: "记录状态", value: "rcdsts" },
      { text: "操作", value: "actions", sortable: false }
    ],
    desserts: [],
    editedIndex: -1,
    editedItem: {
      accprdno: "",
      accprdpno:"",
      seqno: "",
      accfaccode:"",
      accfacchnm: "",
      busnoa:"",
      prinsubflg:"",
      rcdsts:"1",
    },
    defaultItem: {
       accprdno: "",
      accprdpno:"",
      seqno: "",
      accfaccode:"",
      accfacchnm: "",
      busnoa:"",
      prinsubflg:"",
      rcdsts:"1",
    }
  }),

  computed: {
    formTitle() {
      return this.editedIndex === -1 ? "新增" : "修改";
    }
  },

  watch: {
    dialog(val) {
      val || this.close();
    }
  },

  created() {
    this.initialize();
    this.getDict()
  },

  methods: {
    initialize() {
     this.axios
        .get("/acfb07/list")
        .then(response => {
          this.desserts = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
    },
    getDict(){

       this.axios
        .get("/dict/topName/记录状态")
        .then(response => {
          this.rcdstsItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
        this.axios
        .get("/dict/topName/是否本金科目")
        .then(response => {
          this.prinsubflgItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });

        this.axios
        .get("/dict/level/3")
        .then(response => {
          this.accprdpnoItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
        this.axios
        .get("/dict/topName/核算产品")
        .then(response => {
          this.accprdnoItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });

        this.axios
        .get("/dict/topName/记账要素")
        .then(response => {
          this.accfaccodeItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
       
        this.axios
        .get("/subject/list")
        .then(response => {
          this.busnoaItems = response.data.data;
        })
        .catch(response => {
          console.log(response.data.message);
        });
       
    },
    editItem(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog = true;
    },

    deleteItem(item) {
      const index = this.desserts.indexOf(item);
      confirm("确定要删除此条记录吗?") &&
        this.axios.delete('/acfb07/'+item.id)
          .then(res=>{
            this.desserts.splice(index, 1);
          })
        
    },

    close() {
      this.dialog = false;
      this.$nextTick(() => {
        this.editedItem = Object.assign({}, this.defaultItem);
        this.editedIndex = -1;
      });
    },

    save() {

       
      var arr =  this.editedItem.accprdpno
      if(arr.length>0){
        var str = '';
        for(var i=0;i<arr.length;i++){
          if(i<arr.length-1) arr[i]=arr[i]+'_'
          str = str + arr[i]
        }
        this.editedItem.accprdpno=str;
      }
  


       this.axios.post("/acfb07", this.$qs.stringify(this.editedItem)).then(res => {
          this.initialize();
          this.close();
      });
      
    }
  }
};
</script>